/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.longview.io.modules.userinfo.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.longview.io.common.config.Global;
import com.longview.io.common.persistence.Page;
import com.longview.io.common.web.BaseController;
import com.longview.io.common.utils.DateUtils;
import com.longview.io.common.utils.StringUtils;
import com.longview.io.common.utils.excel.ExportExcel;
import com.longview.io.modules.sys.entity.User;
import com.longview.io.modules.userinfo.entity.UserInfo;
import com.longview.io.modules.userinfo.entity.UserInfoExp;
import com.longview.io.modules.userinfo.service.UserInfoService;

/**
 * 用户信息Controller
 * @author zf
 * @version 2018-04-15
 */
@Controller
@RequestMapping(value = "${adminPath}/userinfo/userInfo")
public class UserInfoController extends BaseController {

	@Autowired
	private UserInfoService userInfoService;
	
	@ModelAttribute
	public UserInfo get(@RequestParam(required=false) String id) {
		UserInfo entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = userInfoService.get(id);
		}
		if (entity == null){
			entity = new UserInfo();
		}
		return entity;
	}
	
	@RequiresPermissions("userinfo:userInfo:view")
	@RequestMapping(value = {"list", ""})
	public String list(UserInfo userInfo, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<UserInfo> page = userInfoService.findPage(new Page<UserInfo>(request, response), userInfo); 
		model.addAttribute("page", page);
		return "modules/userinfo/userInfoList";
	}

	@RequiresPermissions("userinfo:userInfo:view")
	@RequestMapping(value = "form")
	public String form(UserInfo userInfo, Model model) {
		model.addAttribute("userInfo", userInfo);
		return "modules/userinfo/userInfoForm";
	}

	@RequiresPermissions("userinfo:userInfo:edit")
	@RequestMapping(value = "save")
	public String save(UserInfo userInfo, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, userInfo)){
			return form(userInfo, model);
		}
		userInfoService.save(userInfo);
		addMessage(redirectAttributes, "保存用户信息成功");
		return "redirect:"+Global.getAdminPath()+"/userinfo/userInfo/?repage";
	}
	
	@RequiresPermissions("userinfo:userInfo:edit")
	@RequestMapping(value = "delete")
	public String delete(UserInfo userInfo, RedirectAttributes redirectAttributes) {
		userInfoService.delete(userInfo);
		addMessage(redirectAttributes, "删除用户信息成功");
		return "redirect:"+Global.getAdminPath()+"/userinfo/userInfo/?repage";
	}
	/**
	 * 导出用户数据
	 * @param user
	 * @param request
	 * @param response
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("userinfo:userInfo:view")
    @RequestMapping(value = "export", method=RequestMethod.POST)
    public String exportFile(UserInfo userInfo, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
            String fileName = "用户数据"+DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
            Page<UserInfo> page = userInfoService.findPage(new Page<UserInfo>(request, response), userInfo); 
    			new ExportExcel("用户数据", UserInfoExp.class).setDataList(page.getList()).write(response, fileName).dispose();
    		return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导出用户失败！失败信息："+e.getMessage());
		}
		return "redirect:" + adminPath + "/userinfo/userInfo/?repage";
    }
	/***
	 * 查询终端编号是否存在
	 */
	@ResponseBody
	@RequestMapping(value = "updateStat")
	public String updateStat(String userid,String usertype) {
		UserInfo userInfo = new UserInfo();
		userInfo.setId(userid);
		userInfo.setUserType(usertype);
		int ret = userInfoService.updateStat(userInfo);
		if(ret >= 0)
			return "true";
		else
			return "false";
	}	
	/***
	 * 地图显示用户
	 */
	@RequiresPermissions("userinfo:userInfo:view")
	@RequestMapping(value = {"userMap"})
	public String userMap(UserInfo userInfo, HttpServletRequest request, HttpServletResponse response, Model model) {
		//model.addAttribute("page", page);
		return "modules/userinfo/userMap2";
	}
	@ResponseBody
	@RequestMapping(value = {"userMapData"})
	public String userMapData(UserInfo userInfo, HttpServletRequest request, HttpServletResponse response, Model model)
	{
		List<UserInfo> list = userInfoService.findAllList(userInfo); 
		return renderString(response, list);
	}
}